﻿var app = angular.module('app', []);

app.controller('EditableRowCtrl', function ($scope, $filter, $http) {

    var loading_count = 2;

    $scope.news = [];
    $scope.pageIndex = 1;
    $scope.pageSize = 10;

    $('#ajax_loader').show();
    $http.get('/api/categoryapi/get').success(function (data) {
        if (data) {
            $scope.myCategory = data[0];
            $scope.categories = data;
        }
        loading_count--;
        if (loading_count == 0) {
            $('#ajax_loader').hide();
            $scope.loadNews();
        }
    })
    .error(function () {
        loading_count--;
        if (loading_count == 0) {
            $('#ajax_loader').hide();
            $scope.loadNews();
        }
    });

    $http.get('/api/locationapi/get').success(function (data) {
        if (data) {
            $scope.myLocation = data[0];
            $scope.locations = data;
        }
        loading_count--;
        if (loading_count == 0) {
            $('#ajax_loader').hide();
            $scope.loadNews();
        }
    })
   .error(function () {
       loading_count--;
       if (loading_count == 0) {
           $('#ajax_loader').hide();
           $scope.loadNews();
       }
   });


    // remove newsItem
    $scope.removeNews = function (index) {
        var delnewsItem = $scope.news[index];
        $('#myModal-' + index).modal('toggle');
        $('.modal-backdrop').remove();
        $('#ajax_loader').show();
        return $http.post('/news/delete?newsId=' + delnewsItem.id).success(function () {
            $('#ajax_loader').hide();
            $scope.news.splice(index, 1);
        }).
        error(function () {
            $('#ajax_loader').hide();
        });
    };


    $scope.loadNews = function () {
        if ($scope.myCategory && $scope.myLocation) {
            var locationId = $scope.myLocation.id;
            var categoryId = $scope.myCategory.id;
            if (categoryId && locationId) {
                $('#ajax_loader').show();
                $http.get('/api/newsapi/GetNewsByCategoryAndLocation?locationid=' + locationId + '&categoryId=' + categoryId + '&pageIndex=' + $scope.pageIndex
                + '&pageSize=' + $scope.pageSize).success(function (data) {
                    if (data && data.length > 0 && data.length == $scope.pageSize) {
                        $scope.news = $scope.news.concat(data);
                        $('.load-more').show();
                        $scope.pageIndex++;
                    } else {
                        $scope.news = $scope.news.concat(data);
                        $('.load-more').hide();
                    }
                    $('#ajax_loader').hide();
                })
                .error(function () {
                    $('#ajax_loader').hide();
                });
            }
        }
    };

    $scope.loadMore = function () {
        if ($scope.myCategory && $scope.myLocation) {
            var locationId = $scope.myLocation.id;
            var categoryId = $scope.myCategory.id;
            if (categoryId && locationId) {
                $('#ajax_loader').show();
                $http.get('/api/newsapi/GetNewsByCategoryAndLocation?locationid=' + locationId + '&categoryId=' + categoryId + '&pageIndex=0').success(function (data) {
                    $scope.news.concat(data);
                    $('#ajax_loader').hide();
                })
                .error(function () {
                    $('#ajax_loader').hide();
                });
            }
        }
    };
});